home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / SCREW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.6 KB  |  125 lines

  1. /* 
  2.  * screw.c
  3.  * 
  4.  * FUNCTION:
  5.  * Draws a screw shape.
  6.  *
  7.  * HISTORY:
  8.  * -- created by Linas Vepstas October 1991
  9.  * -- heavily modified to draw more texas shapes, Feb 1993, Linas
  10.  * -- converted to use GLUT -- December 1995, Linas
  11.  *
  12.  */
  13.  
  14. /* required include files */
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <GL/glut.h>
  18. #include <GL/tube.h>
  19.  
  20. /* =========================================================== */
  21.  
  22. #define SCALE 1.3
  23. #define CONTOUR(x,y) {                    \
  24.    double ax, ay, alen;                    \
  25.    contour[i][0] = SCALE * (x);                \
  26.    contour[i][1] = SCALE * (y);                \
  27.    if (i!=0) {                        \
  28.       ax = contour[i][0] - contour[i-1][0];        \
  29.       ay = contour[i][1] - contour[i-1][1];        \
  30.       alen = 1.0 / sqrt (ax*ax + ay*ay);        \
  31.       ax *= alen;   ay *= alen;                \
  32.       norms [i-1][0] = ay;                \
  33.       norms [i-1][1] = -ax;                \
  34.    }                            \
  35.    i++;                            \
  36. }
  37.  
  38. #define NUM_PTS (25)
  39.  
  40. double contour [NUM_PTS][2];
  41. double norms [NUM_PTS][2];
  42.  
  43. void init_contour (void)
  44. {
  45.    int i;
  46.  
  47.    /* outline of extrusion */
  48.    i=0;
  49.    CONTOUR (1.0, 1.0);
  50.    CONTOUR (1.0, 2.9);
  51.    CONTOUR (0.9, 3.0);
  52.    CONTOUR (-0.9, 3.0);
  53.    CONTOUR (-1.0, 2.9);
  54.  
  55.    CONTOUR (-1.0, 1.0);
  56.    CONTOUR (-2.9, 1.0);
  57.    CONTOUR (-3.0, 0.9);
  58.    CONTOUR (-3.0, -0.9);
  59.    CONTOUR (-2.9, -1.0);
  60.  
  61.    CONTOUR (-1.0, -1.0);
  62.    CONTOUR (-1.0, -2.9);
  63.    CONTOUR (-0.9, -3.0);
  64.    CONTOUR (0.9, -3.0);
  65.    CONTOUR (1.0, -2.9);
  66.  
  67.    CONTOUR (1.0, -1.0);
  68.    CONTOUR (2.9, -1.0);
  69.    CONTOUR (3.0, -0.9);
  70.    CONTOUR (3.0, 0.9);
  71.    CONTOUR (2.9, 1.0);
  72.  
  73.    CONTOUR (1.0, 1.0);   /* repeat so that last normal is computed */
  74. }
  75.    
  76. /* =========================================================== */
  77.  
  78. extern float lastx;
  79. extern float lasty;
  80.  
  81. extern void TextureStyle (int msg);
  82.  
  83. void InitStuff (void)
  84. {
  85.    int style;
  86.  
  87.    /* pick model-vertex-cylinder coords for texture mapping */
  88.    TextureStyle (509);
  89.  
  90.    /* configure the pipeline */
  91.    style = TUBE_JN_CAP;
  92.    style |= TUBE_CONTOUR_CLOSED;
  93.    style |= TUBE_NORM_FACET;
  94.    style |= TUBE_JN_ANGLE;
  95.    gleSetJoinStyle (style);
  96.  
  97.    lastx = 121.0;
  98.    lasty = 121.0;
  99.  
  100.    init_contour();
  101. }
  102.  
  103. /* =========================================================== */
  104.  
  105. void DrawStuff (void) {
  106.  
  107.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  108.    glColor3f (0.5, 0.6, 0.6);
  109.  
  110.    /* set up some matrices so that the object spins with the mouse */
  111.    glPushMatrix ();
  112.    glTranslatef (0.0, 0.0, -80.0);
  113.    glRotatef (130.0, 0.0, 1.0, 0.0);
  114.    glRotatef (65.0, 1.0, 0.0, 0.0);
  115.  
  116.    /* draw the brand and the handle */
  117.    gleScrew (20, contour, norms, 
  118.                  NULL, -6.0, 9.0, lasty);
  119.  
  120.    glPopMatrix ();
  121.    glutSwapBuffers ();
  122. }
  123.  
  124. /* ===================== END OF FILE ================== */
  125.